import { DbConfig } from "../../src/config/DbConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";

export class ChargeDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_account;
    public static Tb_Name: string = "t_charges";


    public static async insertOrder(order_id: string, userid: number, goodid: number, rmb: number, create_time: string, type: string, chargenum: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = 'INSERT INTO ' + this.Tb_Name + '(order_id,userid,goodid,rmb,status,create_time, type,chargenum) VALUES(?,?,?,?,?,?,?,?) ;';
        let sqlParams = [order_id, userid, goodid, rmb, 0, create_time, type, chargenum];
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false
        }
    }

    public static async getOrder(order_id: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "select * from " + this.DB_NAME + "." + this.Tb_Name + " where `order_id`=?;"
        let sqlParams = [order_id]
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results.length > 0) {
            return results[0]
        }
    }

    public static async finishOrder(order_id: string,) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "update " + this.DB_NAME + "." + this.Tb_Name + " set `pay_time`=?, status=1  where `order_id`=?";
        let sqlParams = [DateUtil.formatDateStr(DateUtil.now()), order_id]
        let results = await client.queryPromise(sql, sqlParams);
        if (results && results["affectedRows"] > 0) {
            return true
        }
        else {
            return false;
        }
    }

    public static async clear(delete_time: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "delete from t_charges where status=0 and create_time < ?"
        let sqlParams = [DateUtil.formatDateStr(delete_time)];
        client.queryPromise(sql, sqlParams);
    }


    public static async game_date_charge_data(startTime: string, endTime: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql = `select  t_charges.userid,rmb,chargenum,type,pay_time, nick_name, headimg from t_charges left join t_users on t_charges.userid=t_users.userid where pay_time >= ? and pay_time < ? and status = 1 `
        let sqlParams = [startTime, endTime];
        let results = await client.queryPromise(sql, sqlParams);
        return results
    }

    public static async game_week_charge_data(startTime: string, endTime: string) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql = `select * from t_charges where pay_time >= ? and pay_time < ? and status = 1`
        let sqlParams = [startTime, endTime];
        let results = await client.queryPromise(sql, sqlParams);
        return results
    }


}